package com.sgb.kjwl.viewmodel.manage

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.RegexUtils
import com.sgb.kjwl.api.APIManager
import com.swgk.core.base.api.ICallBack
import com.sgb.link.entity.PickFilterEntity
import com.sgb.kjwl.model.entity.manage.GroupDetailEntity
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity
import com.sgb.kjwl.view.ui.activity.manage.WebSiteEditGroupActivity
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.H5ReEntity
import com.swgk.core.dialog.DialogHelper
import com.swgk.core.util.MToast
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.io.File
import java.util.*
import kotlin.collections.ArrayList


class WebSiteEditGroupModel constructor(activity : WebSiteEditGroupActivity) : BaseViewModel() {
    private var mActivity : WebSiteEditGroupActivity = activity
    var businessType = MutableLiveData<List<PickFilterEntity<List<PickFilterEntity<*>>>>>()
     var urlString = MutableLiveData("")
    
     var cityArea = MutableLiveData(ArrayList<PickFilterEntity<List<PickFilterEntity<*>>>>())

    //经营类型
    fun getBusinessType1() {
        APIManager.getInstance().businessManagementAPI.getNewServiceType(0).enqueue(object :
            ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<*>>>>>>(
                true,
                true
            ) {
            override fun onSuccess(res: BaseEntity<List<PickFilterEntity<List<PickFilterEntity<*>>>>>) {
//                Log.i("----------------","返回结果为:$data")
                if (res?.data != null) {
                    businessType.postValue(res.data)
                }
            }
        })
    }

    fun uploadAvatar(mContext: Context?, file: File) {
        DialogHelper.showProgressDialog(mContext, null, "图片上传中...", 0, false, null)
            .setCanceledOnTouchOutside(true)
        //创建map集合
        val photos: MutableMap<String, RequestBody> = HashMap()
        //将文件转换成多媒体文件，方便上传
        val requestBody = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
        photos["file" + "\"; filename=\"" + RegexUtils.getReplaceAll(file.name, "[\u4e00-\u9fa5]", "")  + "image.jpg\""] = requestBody
        //接口请求
        APIManager.getInstance().uploadAPI.uploadFile(photos).enqueue(object : ICallBack<BaseEntity<*>>(){
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data == null || data.data == null) {
                    MToast.showToast(mContext, "上传失败")
                    return
                }
                urlString.postValue(data.data.toString())
            }

        })
    }


    // -----市场区域--------------------------------------------------------------------------
    fun getMarketArea() {
        DialogHelper.showProgressDialog(mActivity, null, "数据获取中" + "...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().websiteInfoAPI.marketArea.enqueue(object : ICallBack<BaseEntity<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<*>>>>>>>(){
            override fun onSuccess(data: BaseEntity<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<*>>>>>>?) {
                if (data == null || data.data == null || data.data.data == null) {
                    return
                }
                cityArea.postValue(data.data!!.data as ArrayList<PickFilterEntity<List<PickFilterEntity<*>>>>)
            }

            override fun onFail() {
                DialogHelper.dismissProgressDialog()
                super.onFail()
            }
        })
    }


    /**
     * 根据路由获取H5链接访问
     *
     * @param mContext
     * @param body     传入的参数
     * @param param    路由地址
     * @param code     返回的Code码
     */
    fun getH5Url(body: String?, param: String?, code: Int, title: String) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().pickAPI.getUrlSetting(param).enqueue(object : ICallBack<BaseEntity<H5ReEntity>>() {
            override fun onSuccess(data: BaseEntity<H5ReEntity>?) {
                if (data == null || data.state == null) {
                    return
                }
                if (data.state == APIManager.RESPONSE_OK) {
                    val url = data.data.url + "?title=" + title
                    WebViewX5HostActivity.start(mActivity, url, body, code)
                } else {
                    MToast.showToast(mActivity, data.msg)
                }
            }

        })
    }


    fun addOrUpdateGroupMsg(entity : GroupDetailEntity){
        DialogHelper.showProgressDialog(mActivity, null, "保存中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().websiteInfoAPI.addOrUpdateGroupMsg(entity).enqueue(object : ICallBack<BaseEntity<*>>(){
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data != null && data.state == APIManager.RESPONSE_OK) {
                    message.postValue("保存成功")
                }
            }
        })
    }

}